package com.ysd.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;



public class DBmanager {


		private final static String DRIVER="com.mysql.jdbc.Driver";
		private final static String URL="jdbc:mysql://localhost:3306/company";
		private final static String USER="root";
		private final static String PWD="admin";
	public static Connection getConnection() {
		try {
//		1�������� 
			Class.forName(DRIVER);
//		System.out.println("���������ɹ�");
//		2��������
			return (Connection) DriverManager.getConnection(URL, USER, PWD);

		} catch (ClassNotFoundException e) {
		System.out.println("��������ʧ��");
			e.printStackTrace();
		} catch (SQLException e) {
			System.out.println("�û������������");
			e.printStackTrace();
		}
		return null;
			
		}

	public static int executeUpdate(String sql) {
		Connection conn = null; 
		Statement st = null; 
		try { 
			conn = getConnection(); 
			st = conn.createStatement(); 
			int n= st.executeUpdate(sql); 
			return n;
		} catch (SQLException e) {
			e.printStackTrace(); 
			return 0; 
		} finally { 
			closeAll(conn, st, null);
	
		}
	}

		
	public static void closeAll(Connection conn,Statement st, ResultSet rs) {
		try {
//			5�ر���
			if(rs!=null) {
				rs.close();
			} else if (st!=null) {
				st.close();
			}else if(conn!=null) {
				conn.close();
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}
